package world.criver.h50529.function;

import world.criver.h50529.module.moEvent;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;

public class funTcpAndDup {
    public String send(moEvent event){
        try(Socket client=new Socket()){
            funMessageFormat mf=new funMessageFormat();
            client.connect(new InetSocketAddress(event.getIp(),event.getPort()),3000);
            OutputStream out=client.getOutputStream();
            byte[] bs;
            if(event.getMessage_type().equalsIgnoreCase("hex"))bs=mf.hexToByte(event.getSend_data());
            else bs=mf.hexToByte(mf.stringToHex(event.getSend_data()));
            out.write(bs);
            out.flush();
            out.close();
            String line="";
            try {
                BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
                line = in.readLine();
                in.close();
            }catch (Exception ee){line="";}
            return "发送成功。"+(line.isEmpty()?"":"返回："+line);
        }catch (Exception e){
            e.printStackTrace();
            return event.getCommunication_mode()+"连接失败。";
        }
    }
}
